﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.DataVisualization;
using System.Web.UI.DataVisualization.Charting;
using System.Collections.Generic;
using System.Text;

using CE.Global;
using CE.Database;

public partial class monthly_sales_target : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //Session[CE.Global.General.Guest] = null;
        if (Session[CE.Global.General.Guest] == null)
        {
            divSalesTarget.Visible = false;
            divSecurityLogin.Visible = true;
        }
        if (!Page.IsPostBack)
        {
            LoadAgents();
            int _cryr = CE.Global.General.GetUTCDateTime().Year;
            int _styr = 2015;
            for (int _year = _cryr; _year >= _styr; _year--)
            {
                ddlYear.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            }
            ddlMonth.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            if (Session[CE.Global.General.Guest] != null && Session[CE.Global.General.Guest].ToString().ToUpper() == "GGADMIN")
            {
                divSalesTarget.Visible = true;
                divSecurityLogin.Visible = false;
                ddlChartType_SelectedIndexChanged(null, null);
                PageLoad();
            }
            else
            {
                divSalesTarget.Visible = false;
                divSecurityLogin.Visible = true;
            }
        }
    }

    public void DrawChart()
    {
        StringBuilder strScript = new StringBuilder();
        try
        {
            string IsLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXCRMIsCXLive");
            MyDatabase _db = new MyDatabase();
            if (IsLive != "1")
            {
                _db = new MyDatabase("GGR_DBConnectionString_TEST");
            }
            string _spName = "";
            string _ReportDate = ddlYear.Text + ddlMonth.Text;
            DataSet _dsAmount = new DataSet();

            _spName = "sprocCXCRMAgentSalesTargetSelectedItemListByMonth";
            SortedList _paraList = new SortedList();
            _paraList.Add("@ReportDate", _ReportDate);
            _paraList.Add("@LoginType", "2");
            _dsAmount = _db.GetDataSet(_spName, _paraList);

            #region Chart Values
            DataTable _dtAgentSalesNew = new DataTable();
            DataTable _dtAgentSalesRenewal = new DataTable();

            if (_dsAmount != null && _dsAmount.Tables.Count > 0)
            {
                if (_dsAmount.Tables.Count > 1 && _dsAmount.Tables[1].Rows.Count > 0)
                {
                    _dtAgentSalesRenewal = _dsAmount.Tables[1];
                }
            }

            if (_dsAmount != null && _dsAmount.Tables.Count > 0)
            {
                if (_dsAmount.Tables.Count > 0 && _dsAmount.Tables[0].Rows.Count > 0)
                {
                    _dtAgentSalesNew = _dsAmount.Tables[0];
                }
            }

            #endregion

            #region Chart New
            if (_dtAgentSalesNew != null && _dtAgentSalesNew.Rows.Count > 0)
            {
                strScript.Append(@"<script type='text/javascript'>");
                strScript.Append("window.onload = function () {");

                #region Chart New Sales
                strScript.Append(" var chart = new CanvasJS.Chart('chartContainerNew', {");
                strScript.Append("title:{");
                strScript.Append("text:''");
                strScript.Append("},");
                strScript.Append("animationEnabled: true,");
                strScript.Append("axisX:{");
                strScript.Append("interval: 1,");
                strScript.Append("labelFontSize: 12,");
                strScript.Append("lineThickness: 0");
                strScript.Append("},");
                strScript.Append("axisY2:{");
                strScript.Append("valueFormatString: ' 0',");
                strScript.Append("lineThickness: 0  ");
                strScript.Append("},");
                strScript.Append("toolTip: {");
                strScript.Append("shared: true");
                strScript.Append("},");
                strScript.Append("legend:{");
                strScript.Append("verticalAlign: 'top',");
                strScript.Append("horizontalAlign: 'center'");
                strScript.Append("},");
                strScript.Append("data: [");
                strScript.Append("{");
                strScript.Append("type: 'stackedBar',");
                strScript.Append("showInLegend: true,");
                strScript.Append("name: '" + Resources.General.Collection + "',");
                strScript.Append("axisYType: 'secondary',");
                strScript.Append("color: '#85b402',");
                strScript.Append("dataPoints: [");
                foreach (DataRow row in _dtAgentSalesNew.Rows)
                {
                    string _values = "{y: " + row["CollectedAmount"].ToString() + ", label: '" + row["AgentCodeId"].ToString() + "'},";
                    strScript.Append(_values);
                }
                strScript.Append("]");
                strScript.Append(" },");
                strScript.Append("{ ");
                strScript.Append("type: 'stackedBar',");
                strScript.Append("showInLegend: true,");
                strScript.Append("name: '" + Resources.General.TargetBalance + "',");
                strScript.Append("axisYType: 'secondary',");
                strScript.Append("color: '#cccccc',");
                strScript.Append("dataPoints: [");
                foreach (DataRow row in _dtAgentSalesNew.Rows)
                {
                    string _values = "{y: " + row["BalanceAmount"].ToString() + ", label: '" + row["AgentCodeId"].ToString() + "'},";
                    strScript.Append(_values);
                }
                strScript.Append("]");
                strScript.Append("},");
                strScript.Append("{");
                strScript.Append("type: 'stackedBar',");
                strScript.Append("showInLegend: true,");
                strScript.Append("name: '" + Resources.General.OverTraget + "',");
                strScript.Append("axisYType: 'secondary',");
                strScript.Append("color: '#f8d347',");
                strScript.Append("dataPoints: [");
                foreach (DataRow row in _dtAgentSalesNew.Rows)
                {
                    string _values = "{y: " + row["OvertheTarget"].ToString() + ", label: '" + row["AgentCodeId"].ToString() + "'},";
                    strScript.Append(_values);
                }
                strScript.Append("]");
                strScript.Append("}");
                strScript.Append("]");
                strScript.Append("});");
                strScript.Append(" chart.render();");
                #endregion

                #region Chart Renewal Sales
                strScript.Append(" var chartrenewal = new CanvasJS.Chart('chartContainerRenewal', {");
                strScript.Append("title:{");
                strScript.Append("text:''");
                strScript.Append("},");
                strScript.Append("animationEnabled: true,");
                strScript.Append("axisX:{");
                strScript.Append("interval: 1,");
                strScript.Append("labelFontSize: 12,");
                strScript.Append("lineThickness: 0");
                strScript.Append("},");
                strScript.Append("axisY2:{");
                strScript.Append("valueFormatString: ' 0',");
                strScript.Append("lineThickness: 0  ");
                strScript.Append("},");
                strScript.Append("toolTip: {");
                strScript.Append("shared: true");
                strScript.Append("},");
                strScript.Append("legend:{");
                strScript.Append("verticalAlign: 'top',");
                strScript.Append("horizontalAlign: 'center'");
                strScript.Append("},");
                strScript.Append("data: [");
                strScript.Append("{");
                strScript.Append("type: 'stackedBar',");
                strScript.Append("showInLegend: true,");
                strScript.Append("name: '" + Resources.General.Collection + "',");
                strScript.Append("axisYType: 'secondary',");
                strScript.Append("color: '#85b402',");
                strScript.Append("dataPoints: [");
                foreach (DataRow row in _dtAgentSalesRenewal.Rows)
                {
                    string _values = "{y: " + row["CollectedAmount"].ToString() + ", label: '" + row["AgentCodeId"].ToString() + "'},";
                    strScript.Append(_values);
                }
                strScript.Append("]");
                strScript.Append(" },");
                strScript.Append("{ ");
                strScript.Append("type: 'stackedBar',");
                strScript.Append("showInLegend: true,");
                strScript.Append("name: '" + Resources.General.TargetBalance + "',");
                strScript.Append("axisYType: 'secondary',");
                strScript.Append("color: '#cccccc',");
                strScript.Append("dataPoints: [");
                foreach (DataRow row in _dtAgentSalesRenewal.Rows)
                {
                    string _values = "{y: " + row["BalanceAmount"].ToString() + ", label: '" + row["AgentCodeId"].ToString() + "'},";
                    strScript.Append(_values);
                }
                strScript.Append("]");
                strScript.Append("},");
                strScript.Append("{");
                strScript.Append("type: 'stackedBar',");
                strScript.Append("showInLegend: true,");
                strScript.Append("name: '" + Resources.General.OverTraget + "',");
                strScript.Append("axisYType: 'secondary',");
                strScript.Append("color: '#f8d347',");
                strScript.Append("dataPoints: [");
                foreach (DataRow row in _dtAgentSalesRenewal.Rows)
                {
                    string _values = "{y: " + row["OvertheTarget"].ToString() + ", label: '" + row["AgentCodeId"].ToString() + "'},";
                    strScript.Append(_values);
                }
                strScript.Append("]");
                strScript.Append("}");
                strScript.Append("]");
                strScript.Append("});");
                strScript.Append(" chartrenewal.render();");
                #endregion

                strScript.Append("}");
                strScript.Append("</script>");
                ltScripts.Text = strScript.ToString();
            }
            #endregion

            #region Target Details
            string _month = General.GetUTCDateTime().ToString("MMM");
            float _Target = 0.0f;
            float _Collection = 0.0f;
            float _Balance = 0.0f;
            Int16 _Rank = 0;
            float _targetper = 0.0f;
            float _salesper = 0.0f;
            float _balanceper = 0.0f;
            Int32 _TotalWorkingDays = 0;
            Int32 _RemainWorkingDays = 0;
            Int32 _PastWorkingDays = 0;
            float _DailyTarget = 0.0f;
            float _TodayTarget = 0.0f;
            float _TodayCollection = 0.0f;
            float _LastYearCollection = 0.0f;
            string _LastYearMonth = "Collection in ";
            Int32 _NewSales = 0;
            Int32 _RenewalSales = 0;
            Int32 _OtherSales = 0;
            float _NewCollection = 0.0f;
            float _RenewalCollection = 0.0f;
            float _OtherCollection = 0.0f;
            Int32 _NewSalesPer = 0;
            Int32 _RenewalSalesPer = 0;
            Int32 _OtherSalesPer = 0;

            int targetpercent = 0;
            string targetpercentdisplay = targetpercent.ToString() + "%";
            string targetcolor = "";
            int salepercent = 0;
            string salepercentdisplay = salepercent.ToString() + "%";
            string salecolor = "";
            int balancepercent = 0;
            string balancepercentdisplay = balancepercent.ToString() + "%";
            string balancecolor = "";

            if (_dsAmount != null && _dsAmount.Tables.Count > 3 && _dsAmount.Tables[3].Rows.Count > 0)
            {
                foreach (DataRow row in _dsAmount.Tables[3].Rows)
                {
                    foreach (DataColumn dc in _dsAmount.Tables[3].Columns)
                    {
                        string MonthCol = dc.ColumnName;
                        if (MonthCol == _month)
                        {
                            _Target = Convert.ToSingle(row[dc].ToString());
                        }
                    }
                }
            }

            if (_dsAmount != null && _dsAmount.Tables.Count > 4 && _dsAmount.Tables[4].Rows.Count > 0)
            {
                _Collection = Convert.ToSingle(_dsAmount.Tables[4].Rows[0]["IndividualAmount"].ToString());
                _Rank = Convert.ToInt16(_dsAmount.Tables[4].Rows[0]["AgentRank"].ToString());
                _TotalWorkingDays = Convert.ToInt32(_dsAmount.Tables[4].Rows[0]["TotalWorkingDays"].ToString());
                _RemainWorkingDays = Convert.ToInt32(_dsAmount.Tables[4].Rows[0]["RemainWorkingDays"].ToString());
                _PastWorkingDays = _TotalWorkingDays - _RemainWorkingDays;
            }

            if (_dsAmount != null && _dsAmount.Tables.Count > 5 && _dsAmount.Tables[5].Rows.Count > 0)
            {
                if (!float.TryParse(_dsAmount.Tables[5].Rows[0]["TodayCollection"].ToString(), out _TodayCollection))
                {
                    _TodayCollection = 0.0f;
                }
            }

            if (_Collection > 0)
            {
                _Balance = Convert.ToSingle(_Target - _Collection);
            }

            lblLastYearCollection.Text = "";
            lblLastYearCollectionTitle.Text = "";
            if (ddlChartType.Text == "M")
            {
                if (_dsAmount != null && _dsAmount.Tables.Count > 6 && _dsAmount.Tables[6].Rows.Count > 0)
                {
                    if (!float.TryParse(_dsAmount.Tables[6].Rows[0]["LastYearCollection"].ToString(), out _LastYearCollection))
                    {
                        _LastYearCollection = 0.0f;
                    }
                    _LastYearMonth += _dsAmount.Tables[6].Rows[0]["MonthOfLastYear"].ToString();
                    lblLastYearCollection.Text = _LastYearCollection.ToString("#,000.00");
                    lblLastYearCollectionTitle.Text = _LastYearMonth + " : ";
                }
            }

            lblTotalTargetTitle.Text = Resources.General.TotalTarget;
            lblTotalCollectionTitle.Text = Resources.General.TotalCollection;
            lblTargetBalanceTitle.Text = Resources.General.TargetBalance;
            lblTotalTarget.Text = Math.Ceiling(_Target).ToString("#,000.00");
            lblTotalCollection.Text = Math.Floor(_Collection).ToString("#,000.00");
            lblTargetBalance.Text = Math.Ceiling(_Balance).ToString("#,000.00");

            #region Sales count and Amount
            lblNewSalesCount.Text = "0";
            lblRenewalSalesCount.Text = "0";
            lblOtherSalesCount.Text = "0";
            lblNewSalesAmount.Text = "0.00";
            lblRenewalSalesAmount.Text = "0.00";
            lblOtherSalesAmount.Text = "0.00";
            if (ddlChartType.Text == "M")
            {
                if (_dsAmount != null && _dsAmount.Tables.Count > 7 && _dsAmount.Tables[7].Rows.Count > 0)
                {
                    if (!Int32.TryParse(_dsAmount.Tables[7].Rows[0]["NewCount"].ToString(), out _NewSales))
                    {
                        _NewSales = 0;
                    }
                    if (!Int32.TryParse(_dsAmount.Tables[7].Rows[0]["RenewalCount"].ToString(), out _RenewalSales))
                    {
                        _RenewalSales = 0;
                    }
                    if (!Int32.TryParse(_dsAmount.Tables[7].Rows[0]["OtherCount"].ToString(), out _OtherSales))
                    {
                        _OtherSales = 0;
                    }
                    if (!float.TryParse(_dsAmount.Tables[7].Rows[0]["NewAmount"].ToString(), out _NewCollection))
                    {
                        _NewCollection = 0.0f;
                    }
                    if (!float.TryParse(_dsAmount.Tables[7].Rows[0]["RenewalAmount"].ToString(), out _RenewalCollection))
                    {
                        _RenewalCollection = 0.0f;
                    }
                    if (!float.TryParse(_dsAmount.Tables[7].Rows[0]["OtherAmount"].ToString(), out _OtherCollection))
                    {
                        _OtherCollection = 0.0f;
                    }
                    if (_NewCollection > 0)
                    {
                        float _newpercent = 0.0f;
                        _newpercent = (_NewCollection / _Collection) * 100;
                        _NewSalesPer = Convert.ToInt32(_newpercent);
                    }
                    if (_RenewalCollection > 0)
                    {
                        float _renewalpercent = 0.0f;
                        _renewalpercent = (_RenewalCollection / _Collection) * 100;
                        _RenewalSalesPer = Convert.ToInt32(_renewalpercent);
                    }
                    if (_OtherCollection > 0)
                    {
                        float _otherpercent = 0.0f;
                        _otherpercent = (_OtherCollection / _Collection) * 100;
                        _OtherSalesPer = Convert.ToInt32(_otherpercent);
                    }

                    lblNewSalesCount.Text = _NewSales.ToString();
                    lblRenewalSalesCount.Text = _RenewalSales.ToString();
                    lblOtherSalesCount.Text = _OtherSales.ToString();

                    lblNewSalesPer.Text = _NewSalesPer.ToString() + "%";
                    lblRenewalSalesPer.Text = _RenewalSalesPer.ToString() + "%";
                    lblOtherSalesPer.Text = _OtherSalesPer.ToString() + "%";

                    lblNewSalesAmount.Text = _NewCollection.ToString("#,000.00");
                    lblRenewalSalesAmount.Text = _RenewalCollection.ToString("#,000.00");
                    lblOtherSalesAmount.Text = _OtherCollection.ToString("#,000.00");

                    #region Percentage     
                    //string newcolor = "";
                    //string renewalcolor = "";
                    //string othercolor = "";
                    //string newperdisplay = _NewSalesPer.ToString() + "%";
                    //string renewalperdisplay = _RenewalSalesPer.ToString() + "%";
                    //string otherperdisplay = _OtherSalesPer.ToString() + "%";

                    ////New
                    //if (_NewSalesPer <= 25)
                    //{
                    //    newcolor = "#ff2c2c";
                    //}
                    //else if (_NewSalesPer > 25 && _NewSalesPer <= 50)
                    //{
                    //    newcolor = "#ff9212";
                    //}
                    //else if (_NewSalesPer > 50 && _NewSalesPer <= 75)
                    //{
                    //    newcolor = "#ffe012";
                    //}
                    //else if (_NewSalesPer > 75)
                    //{
                    //    newcolor = "#71C73E";
                    //}

                    ////Other
                    //if (_OtherSalesPer <= 25)
                    //{
                    //    othercolor = "#ff2c2c";
                    //}
                    //else if (_OtherSalesPer > 25 && _OtherSalesPer <= 50)
                    //{
                    //    othercolor = "#ff9212";
                    //}
                    //else if (_OtherSalesPer > 50 && _OtherSalesPer <= 75)
                    //{
                    //    othercolor = "#ffe012";
                    //}
                    //else if (_OtherSalesPer > 75)
                    //{
                    //    othercolor = "#71C73E";
                    //}

                    ////Renewal
                    //if (_RenewalSalesPer <= 25)
                    //{
                    //    renewalcolor = "#ff2c2c";
                    //}
                    //else if (_RenewalSalesPer > 25 && _RenewalSalesPer <= 50)
                    //{
                    //    renewalcolor = "#ff9212";
                    //}
                    //else if (_RenewalSalesPer > 50 && _RenewalSalesPer <= 75)
                    //{
                    //    renewalcolor = "#ffe012";
                    //}
                    //else if (_RenewalSalesPer > 75)
                    //{
                    //    renewalcolor = "#71C73E";
                    //}

                    //divNewSalesPer.Attributes.Add("data-text", newperdisplay);
                    //divNewSalesPer.Attributes.Add("data-percent", _NewSalesPer.ToString());
                    //divNewSalesPer.Attributes.Add("data-fgcolor", newcolor);

                    //divOtherSalesPer.Attributes.Add("data-text", otherperdisplay);
                    //divOtherSalesPer.Attributes.Add("data-percent", _OtherSalesPer.ToString());
                    //divOtherSalesPer.Attributes.Add("data-fgcolor", othercolor);

                    //divRenewalSalesPer.Attributes.Add("data-text", renewalperdisplay);
                    //divRenewalSalesPer.Attributes.Add("data-percent", _RenewalSalesPer.ToString());
                    //divRenewalSalesPer.Attributes.Add("data-fgcolor", renewalcolor);
                    #endregion
                }
            }
            #endregion

            _DailyTarget = _Target / _TotalWorkingDays;
            _TodayTarget = _DailyTarget;
            if (_Target > 0)
            {
                _targetper = 100;
            }
            if (_Target > 0 && _Collection > 0)
            {
                _salesper = (_Collection / _Target) * 100;
            }
            if (_Target > 0 && _Balance > 0)
            {
                _balanceper = (_Balance / _Target) * 100;
            }

            #region Sales & Target - Values
            targetpercent = Convert.ToInt32(_targetper);
            targetpercentdisplay = targetpercent.ToString() + "%";
            targetcolor = "";
            salepercent = Convert.ToInt32(_salesper);
            salepercentdisplay = salepercent.ToString() + "%";
            salecolor = "";
            balancepercent = Convert.ToInt32(_balanceper);
            balancepercentdisplay = balancepercent.ToString() + "%";
            balancecolor = "";


            //Target
            if (targetpercent <= 25)
            {
                targetcolor = "#ff2c2c";
            }
            else if (targetpercent > 25 && targetpercent <= 50)
            {
                targetcolor = "#ff9212";
            }
            else if (targetpercent > 50 && targetpercent <= 75)
            {
                targetcolor = "#ffe012";
            }
            else if (targetpercent > 75)
            {
                targetcolor = "#71C73E";
            }

            //Sales
            if (salepercent <= 25)
            {
                salecolor = "#ff2c2c";
            }
            else if (salepercent > 25 && salepercent <= 50)
            {
                salecolor = "#ff9212";
            }
            else if (salepercent > 50 && salepercent <= 75)
            {
                salecolor = "#ffe012";
            }
            else if (salepercent > 75)
            {
                salecolor = "#71C73E";
            }

            //Balance
            if (balancepercent <= 25)
            {
                balancecolor = "#71C73E";
            }
            else if (balancepercent > 25 && balancepercent <= 50)
            {
                balancecolor = "#ffe012";
            }
            else if (balancepercent > 50 && balancepercent <= 75)
            {
                balancecolor = "#ff9212";
            }
            else if (balancepercent > 75)
            {
                balancecolor = "#ff2c2c";
            }
            divCollection.Attributes.Add("data-text", salepercentdisplay);
            divCollection.Attributes.Add("data-percent", salepercent.ToString());
            divCollection.Attributes.Add("data-fgcolor", salecolor);
            divBalance.Attributes.Add("data-text", balancepercentdisplay);
            divBalance.Attributes.Add("data-percent", balancepercent.ToString());
            divBalance.Attributes.Add("data-fgcolor", balancecolor);
            #endregion

            #endregion
        }
        catch (Exception ex)
        {
        }
        finally
        {

        }
    }

    public void DrawChartByAgentId()
    {
        StringBuilder strScript = new StringBuilder();
        try
        {
            string IsLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXCRMIsCXLive");
            MyDatabase _db = new MyDatabase();
            if (IsLive != "1")
            {
                _db = new MyDatabase("GGR_DBConnectionString_TEST");
            }
            string _spName = "";
            DataSet _dsAmount = new DataSet();
            Int64 intLoginUserId = Convert.ToInt64(ddlAgent.Text);
            _spName = "sprocCXCRMAgentSalesRankSelectedItemList";
            SortedList _paraList = new SortedList();
            _paraList.Add("@LoginAgentId", intLoginUserId);
            _dsAmount = _db.GetDataSet(_spName, _paraList);

            #region Chart Values
            DataTable _dtAgentSales = new DataTable();
            DataTable _dtAgentChart = new DataTable();
            if (_dsAmount != null && _dsAmount.Tables.Count > 0)
            {
                if (_dsAmount.Tables.Count > 3 && _dsAmount.Tables[3].Rows.Count > 0)
                {
                    _dtAgentChart = _dsAmount.Tables[3];
                }
            }
            #endregion

            #region Chart
            if (_dtAgentChart != null && _dtAgentChart.Rows.Count > 0)
            {
                string _Title = Resources.General.AgentSales;
                strScript.Append(@"<script type='text/javascript'>");
                strScript.Append("window.onload = function () {");

                #region Chart New Sales
                strScript.Append(" var chart = new CanvasJS.Chart('chartContainer', {");
                strScript.Append("title:{");
                strScript.Append("text:'" + _Title + "'");
                strScript.Append("},");
                strScript.Append("animationEnabled: true,");
                strScript.Append("axisX:{");
                strScript.Append("interval: 1,");
                strScript.Append("labelFontSize: 12,");
                strScript.Append("lineThickness: 0");
                strScript.Append("},");
                strScript.Append("axisY2:{");
                strScript.Append("valueFormatString: ' 0',");
                strScript.Append("lineThickness: 0  ");
                strScript.Append("},");
                strScript.Append("toolTip: {");
                strScript.Append("shared: true");
                strScript.Append("},");
                strScript.Append("legend:{");
                strScript.Append("verticalAlign: 'top',");
                strScript.Append("horizontalAlign: 'center'");
                strScript.Append("},");
                strScript.Append("data: [");
                strScript.Append("{");
                strScript.Append("type: 'stackedBar',");
                strScript.Append("showInLegend: true,");
                strScript.Append("name: '" + Resources.General.Collection + "',");
                strScript.Append("axisYType: 'secondary',");
                strScript.Append("color: '#85b402',");
                strScript.Append("dataPoints: [");
                foreach (DataRow row in _dtAgentChart.Rows)
                {
                    string _values = "{y: " + row["IndCollectedAmount"].ToString() + ", label: '" + row["NameofMonth"].ToString() + "'},";
                    strScript.Append(_values);
                }
                strScript.Append("]");
                strScript.Append(" },");
                strScript.Append("{ ");
                strScript.Append("type: 'stackedBar',");
                strScript.Append("showInLegend: true,");
                strScript.Append("name: '" + Resources.General.TargetBalance + "',");
                strScript.Append("axisYType: 'secondary',");
                strScript.Append("color: '#cccccc',");
                strScript.Append("dataPoints: [");
                foreach (DataRow row in _dtAgentChart.Rows)
                {
                    string _values = "{y: " + row["BalanceAmount"].ToString() + ", label: '" + row["NameofMonth"].ToString() + "'},";
                    strScript.Append(_values);
                }
                strScript.Append("]");
                strScript.Append("},");
                strScript.Append("{");
                strScript.Append("type: 'stackedBar',");
                strScript.Append("showInLegend: true,");
                strScript.Append("name: '" + Resources.General.OverTraget + "',");
                strScript.Append("axisYType: 'secondary',");
                strScript.Append("color: '#f8d347',");
                strScript.Append("dataPoints: [");
                foreach (DataRow row in _dtAgentChart.Rows)
                {
                    string _values = "{y: " + row["OvertheTarget"].ToString() + ", label: '" + row["NameofMonth"].ToString() + "'},";
                    strScript.Append(_values);
                }
                strScript.Append("]");
                strScript.Append("}");
                strScript.Append("]");
                strScript.Append("});");
                strScript.Append(" chart.render();");
                #endregion

                strScript.Append("}");
                strScript.Append("</script>");
            }
            ltScriptsAgent.Text = strScript.ToString();
            #endregion


        }
        catch (Exception ex)
        {
        }
        finally
        {

        }
    }

    public void PageLoad()
    {
        if (ddlChartType.Text == "M")
        {
            ltScriptsAgent.Text = "";
            DrawChart();
        }
        else if (ddlChartType.Text == "A")
        {
            ltScripts.Text = "";
            DrawChartByAgentId();
        }
    }

    protected void btnSignIn_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            if (txtSiteUser.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXCRMAdminID"))
            {
                if (txtSiteUserAuthCode.Text == CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXCRMAdminPass"))
                {
                    Session[CE.Global.General.Guest] = "GGADMIN";
                    divSalesTarget.Visible = true;
                    divSecurityLogin.Visible = false;
                    ddlChartType_SelectedIndexChanged(null, null);
                    PageLoad();
                }
                else
                {
                    divSalesTarget.Visible = false;
                    divSecurityLogin.Visible = true;
                }
            }
        }
    }

    protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        PageLoad();
    }

    protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        PageLoad();
    }

    protected void ddlChartType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlChartType.Text == "M")
        {
            divMenu.Attributes.Add("class", "twelve columns");
            divMonthwise.Visible = true;
            divMonthSalesDisplay.Visible = true;
            divMonthSalesGraph.Visible = true;
            divAgentwise.Visible = false;
            divAgentSalesGraph.Visible = false;
            divMonthSalesCount.Visible = true;
        }
        else if (ddlChartType.Text == "A")
        {
            ddlAgent.SelectedIndex = 0;
            divMenu.Attributes.Add("class", "twelve columns");
            divMonthwise.Visible = false;
            divMonthSalesDisplay.Visible = false;
            divMonthSalesGraph.Visible = false;
            divAgentwise.Visible = true;
            divAgentSalesGraph.Visible = true;
            divMonthSalesCount.Visible = false;
        }
        PageLoad();
    }

    protected void ddlAgent_SelectedIndexChanged(object sender, EventArgs e)
    {
        DrawChartByAgentId();
    }

    public void LoadAgents()
    {
        try
        {
            string IsLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXCRMIsCXLive");
            MyDatabase _db = new MyDatabase();
            if (IsLive != "1")
            {
                _db = new MyDatabase("GGR_DBConnectionString_TEST");
            }
            string _spName = "";
            DataTable _dtAgent = new DataTable();

            _spName = "sprocCXCRMSalesTargetAgentList";
            _dtAgent = _db.GetDataTableBySP(_spName);
            if (_dtAgent != null && _dtAgent.Rows.Count > 0)
            {
                ddlAgent.Items.Clear();
                for (int i = 0; i < _dtAgent.Rows.Count; i++)
                {
                    ddlAgent.Items.Add(new ListItem(_dtAgent.Rows[i]["Agents"].ToString(), _dtAgent.Rows[i]["AgentId"].ToString()));
                }
            }
            else
            {
                ddlAgent.Items.Clear();
                ddlAgent.Items.Add(new ListItem("Select", "-1"));
            }
        }
        catch (Exception ex)
        {
            ddlAgent.Items.Clear();
            ddlAgent.Items.Add(new ListItem("Select", "-1"));
        }
        finally
        {

        }
    }

}
